package com.ironman.compute;

import com.ironman.common.structure.Bi7Consumer;
import com.ironman.event.topic.dami.WindTopic;
import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;
import org.noear.snack.ONode;

import java.util.Date;

/**
 * 风矢量计算
 */
@Slf4j
public class WindComputer {

    private final String followTrainingBoatCode;

    public WindComputer(String followTrainingBoatCode) {
        this.followTrainingBoatCode = followTrainingBoatCode;
    }

    public void init(Bi7Consumer<String, Float, Double, Integer, Double, Integer, Date> windConsumer) {
        Dami.<String, Void>bus().listen(WindTopic.GW_WIND_TOPIC, payload -> {
            ONode node = ONode.loadStr(payload.getContent());
            String trainingBoatCode = node.select("windSourceBoatCode").getString();
            if (!followTrainingBoatCode.equals(trainingBoatCode)) {
                return;
            }
            float declination = node.select("declination").getFloat();
            double windSpeed = node.select("windSpeed").getDouble();
            int windDirection = node.select("windDirection").getInt();
            double customSpeed = node.select("customWindSpeed").getDouble();
            int customDirection = node.select("customWindDirection").getInt();
            Date ts = node.select("ts").getDate();
            windConsumer.accept(trainingBoatCode, declination, windSpeed, windDirection, customSpeed, customDirection, ts);
        });
    }
}
